local TweenUtilities = require(script.Parent.TweenUtilities)
local createSignal = require(script.Parent.createSignal)

local Track = {}
Track.__index = Track

local function checkNil(val)
	if val == nil then
		error("Expected ':', not '.', when calling this function.")
	end
end

function Track.new(root, tweenTable, module)
	local self = {
		playing = false,
		DonePlaying = Instance.new("BindableEvent"),
		tweenUtils = TweenUtilities.new(root, tweenTable, module),
		mIsPlaying=false
	}
	self.Completed = self.DonePlaying.Event
	setmetatable(self, Track)
	return self
end

function Track:SetOrigin(origin)
	if origin == nil or typeof(origin) == "CFrame" then
		self.tweenUtils:SetOrigin(origin)
	else
		self.tweenUtils:SetOrigin(CFrame.new(origin))
	end
	
end

function Track:UpdateOrigin(origin)
	--print("*******************")
	--if origin == nil or typeof(origin) == "CFrame" then
	--	self.tweenUtils:UpdateOrigin(origin)
	--else
	--	self.tweenUtils:UpdateOrigin(CFrame.new(origin))
	--end
	--print("---->UpdateOrigin--:",origin)
	--if not O then
	--	O=origin
	--end
	self:SetOrigin(origin)
end

function Track:Play()
	checkNil(self)
	self.playing = true
	self.tweenUtils:ResetValues()
	self.tweenUtils:CreateTweens()
	self.tweenUtils:PlayTweens(function()
		self:OnDonePlaying()
	end)
end

function Track:Loop(cycles)
	checkNil(self)
	if cycles ~= nil and cycles == 0 then
		self:OnDonePlaying()
		return
	end
	self.playing = true
	self.tweenUtils:ResetValues()
	local finished = createSignal()
	local disconnect = finished:subscribe(function()
		if self.playing then
			self:Loop(cycles and cycles - 1)
		else
			self:OnDonePlaying()
		end
	end)
	self.tweenUtils:PlayTweens(function()
		if finished then
			finished:fire()
			disconnect()
			finished = nil
		end
	end)
end

function Track:Loop2() --没有事件监听
	--self.tweenUtils:ResetValues()
	--warn("Loop2",self.tweenUtils.root.Name)

	self.mIsPlaying=true

	--self.tweenUtils:PlayLoop()
	self:Loop()
end

function Track:Stop()
	checkNil(self)
	self.playing = false
	self.tweenUtils:PauseTweens()
	self.tweenUtils:ResetValues()
end


function Track:Pause()  --暂停
	
	--warn("Pause",self.tweenUtils.root.Name)
	
	
	if not self.mIsPlaying then
		return
	end

	self.mIsPlaying=false
	
	--warn("暂停播放",self.tweenUtils.root.Name)

	checkNil(self)
	self.playing = false
	self.tweenUtils:PauseTweens()
	self.tweenUtils:DestroyTween()
	--self.tweenUtils:ResetValues()
	--for _, tween in pairs(self.tweenUtils.tweens) do
	--	for _, track in pairs(tween) do
	--		for _, props in pairs(track) do
	--			local tween = props.Tween
	--			tween:Pause()
	--		end
	--	end
	--end


	--for k,v in pairs(self.tweenUtils.tweens) do
	--	print(k)

	--	--v:Resume()
	--end
end

function Track:Resume()  --继续
	
	if  self.mIsPlaying then
		return
	end
	
	self.mIsPlaying=true
		self.tweenUtils:CreateTweens()
	self:Loop()

end


function Track:OnDonePlaying()
	self.DonePlaying:Fire()
end


function Track:Destroy ( )  --销毁

	self.DonePlaying:Destroy()
	self.tweenUtils:Destroy()

end

return Track
